<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2 class="login-title">DMS系统登录</h2>
      </template>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0"
        class="login-form"
      >
        <!-- 角色选择 -->
        <el-form-item prop="roleType">
          <el-radio-group v-model="loginForm.roleType" class="role-select">
            <el-radio-button value="admin">管理员</el-radio-button>
            <el-radio-button value="merchant">商家</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            :placeholder="loginForm.roleType === 'admin' ? '管理员账号' : '商家账号'"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const loginForm = ref({
  username: '',
  password: '',
  roleType: 'admin'
})

const loginFormRef = ref(null)
const loading = ref(false)

const loginRules = {
  roleType: [
    { required: true, message: '请选择登录角色', trigger: 'change' }
  ],
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    // 表单验证
    await loginFormRef.value.validate()
    loading.value = true
    
    // 调用登录接口
    const loginResult = await userStore.login(
      loginForm.value.username,
      loginForm.value.password,
      loginForm.value.roleType
    )

    // 检查登录结果
    if (!loginResult || !loginResult.roleId) {
      throw new Error('登录失败：无效的用户信息')
    }

    // 根据后端返回的roleId判断跳转路径
    const redirectPath = loginResult.roleId === 1 ? '/admin/home' : '/merchant/home'
    
    // 获取重定向地址或使用默认地址
    const redirect = router.currentRoute.value.query.redirect || redirectPath

    // 登录成功提示
    ElMessage.success('登录成功')
    
    // 跳转到对应页面
    await router.push(redirect)
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败，请检查账号密码是否正确')
    // 确保清理任何可能的登录状态
    await userStore.clearUserInfo()
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.login-card {
  width: 400px;
}

.login-title {
  text-align: center;
  color: #303133;
  margin: 0;
}

.login-form {
  padding: 20px 0;
}

.role-select {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
}
</style> 